home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / PICSee Dust 1.01 / Quaternary Source / Monitors.cpp < prev    next >
Text File  |  1995-11-02  |  10KB  |  341 lines

  1. /*
  2.     Written by Norman Basham, Feb. 1994
  3.     With very minor changes by Hiep Dam, Sept. 1994
  4. */
  5.  
  6. //    ------------------------------------------------------------------------
  7.     #include <Script.h>                    //    GetMBarHeight ()
  8. //    #include <QDOffscreen.h>
  9.     #include "Monitors.h"
  10.  
  11. //    UTILITY STUFF
  12.     short abs (short i);    
  13.  
  14. //    ------------------------------------------------------------------------
  15. void Place (WindowPtr wp, short place)
  16. {
  17.     short                    top;
  18.     short                    left;
  19.     
  20.     switch (place)
  21.         {
  22.         case kOnBigDevice:
  23.             GetCenteredWindowTopLeft (wp, (**GetBigDevice ()).gdRect, &top, &left);
  24.             break;
  25.         
  26.         case kOnDeepDevice:
  27.             top = GetDeepDeviceGRect().top + wp->portRect.top + GetDragBarHeight (wp);
  28.             if (GetMainDevice () == GetMaxDevice (&(**GetGrayRgn ()).rgnBBox))
  29.                 top += GetDragBarHeight (wp);
  30.             left = GetDeepDeviceGRect().left + wp->portRect.left;
  31.             break;
  32.         
  33.         case kCenterOnDeepDevice:
  34.             GetCenteredWindowTopLeft (wp, GetDeepDeviceGRect(), &top, &left);
  35.             break;
  36.         
  37.         case kOnMainDevice:
  38.             top = (**GetMainDevice ()).gdRect.top + wp->portRect.top + GetDragBarHeight (wp) + GetDragBarHeight (wp);
  39.             left = (**GetMainDevice ()).gdRect.left + wp->portRect.left;
  40.             break;
  41.         
  42.         case kCenterOnMainDevice:
  43.             GetCenteredWindowTopLeft (wp, (**GetMainDevice ()).gdRect, &top, &left);
  44.             break;
  45.         
  46.         }
  47.     Move (wp, top, left);
  48.     ShowWindow (wp);
  49. }
  50.  
  51. //    ------------------------------------------------------------------------
  52. void GetCenteredWindowTopLeft (WindowPtr wp, const Rect screenRect, short *top, short *left)
  53.     {
  54.     Rect            rectToCenter = wp->portRect;
  55.     Point            p;
  56.     short            centerOnRectH;
  57.     short            centerOnRectV;
  58.     short            rectToCenterH;
  59.     short            rectToCenterV;
  60.     
  61.     centerOnRectH = abs (screenRect.right - screenRect.left);
  62.     centerOnRectV = abs (screenRect.bottom - screenRect.top);
  63.     rectToCenterH = abs (rectToCenter.right - rectToCenter.left);
  64.     rectToCenterV = abs (rectToCenter.bottom - rectToCenter.top);
  65.     
  66.     *left = screenRect.left + (centerOnRectH - rectToCenterH)/2;
  67.     *top = screenRect.top + (centerOnRectV - rectToCenterV)/2;
  68.     p.h = *left;
  69.     p.v = *top;
  70.     if (PtInRect (p, &(**GetMainDevice ()).gdRect))
  71.         *top +=  GetDragBarHeight (wp)/2;
  72.         
  73.     *top += GetDragBarHeight (wp)/2;
  74.     }
  75.     
  76.  
  77. //    ------------------------------------------------------------------------
  78. void Move (WindowPtr wp, short top, short left)
  79. {
  80.     Boolean            bringToFront = false;
  81.     
  82.     MoveWindow (wp, left, top, bringToFront);
  83. }
  84.  
  85. //    ------------------------------------------------------------------------
  86. short GetDragBarHeight (WindowPtr wp)
  87. {
  88.     WindowPeek        wpPeek = (WindowPeek) wp;
  89.     short            height = 19;
  90.     
  91.     if (wpPeek->visible)                // otherwise these values can be bogus
  92.         height = (**wpPeek->contRgn).rgnBBox.top - (**wpPeek->strucRgn).rgnBBox.top;
  93.         
  94.     return height;
  95. }
  96.  
  97.  
  98. //    ------------------------------------------------------------------------
  99. //    DEVICE STUFF
  100. //    Norman Basham, February 1994
  101. //    ------------------------------------------------------------------------
  102.  
  103.  
  104. //    CODE
  105. //    ------------------------------------------------------------------------
  106.  
  107.  
  108. //    ------------------------------------------------------------------------
  109. //    Get the rect in global coords of passed device
  110. //    ------------------------------------------------------------------------
  111. Rect GetDeviceGRect (GDHandle gdh)
  112. {
  113.     return (**GetMaxDevice (&(**gdh).gdRect)).gdRect;
  114. }
  115.  
  116. //    ------------------------------------------------------------------------
  117. //    Get the monitor with the most depth and return its rect in global coords
  118. //    ------------------------------------------------------------------------
  119. Rect GetDeepDeviceGRect ()
  120. {
  121.     GDHandle            gdh = GetMaxDevice (&(**GetGrayRgn ()).rgnBBox);
  122.     
  123.     return (**GetMaxDevice (&(**gdh).gdRect)).gdRect;
  124. }
  125.  
  126. // ---------------------------------------------------------------------------
  127. // Get deepest device
  128. // ---------------------------------------------------------------------------
  129.  
  130. GDHandle GetDeepestDevice() {
  131.     GDHandle gdh;
  132.     gdh = GetMaxDevice (&(**GetGrayRgn ()).rgnBBox);
  133.     return(gdh);
  134. } // END GetDeepestDevice
  135.  
  136. //    ------------------------------------------------------------------------
  137. //    Given a device, return wether it is a monitor and wether it's active
  138. //    ------------------------------------------------------------------------
  139. Boolean IsMainDevice
  140.     (
  141.     GDHandle            theDevice
  142.     )
  143.     {
  144.     return (theDevice == GetMainDevice ());
  145.     }
  146.  
  147. //    ------------------------------------------------------------------------
  148. //    Given a device, return wether it is a monitor and wether it's active
  149. //    ------------------------------------------------------------------------
  150. Boolean IsActiveScreenDevice
  151.     (
  152.     GDHandle            theDevice
  153.     )
  154.     {
  155.         return (
  156.                 (TestDeviceAttribute (theDevice, screenDevice)) &&
  157.                 (TestDeviceAttribute (theDevice, screenActive))
  158.                 );
  159.     }
  160.  
  161. //    ------------------------------------------------------------------------
  162. //    Given a device, return its depth
  163. //    ------------------------------------------------------------------------
  164. short GetDeviceDepth (GDHandle gdh)
  165. {
  166.     return (**(**gdh).gdPMap).pixelSize;
  167. }
  168.  
  169. //    ------------------------------------------------------------------------
  170. //    Given a depth, GetDeviceWithThisDepth gets the first device with that
  171. //    depth, we check main device first.
  172. //    ------------------------------------------------------------------------
  173. GDHandle GetDeviceWithThisDepth (short depth)
  174. {
  175.     GDHandle            aGDevice;
  176.  
  177.     aGDevice = GetDeviceList();
  178.     while (aGDevice != nil)
  179.         {
  180.         if (depth == GetDeviceDepth(aGDevice))
  181.             return aGDevice;
  182.         
  183.         aGDevice = GetNextDevice (aGDevice);
  184.         }
  185.         
  186.     return nil;
  187. }
  188.  
  189. //    ------------------------------------------------------------------------
  190. //    Get the biggest monitor
  191. //    ------------------------------------------------------------------------
  192. GDHandle GetBigDevice (void)
  193. {
  194.     GDHandle            aGDevice = nil;
  195.     GDHandle            bigGDevice = nil;
  196.     long                biggestArea = 0L;
  197.  
  198.     aGDevice = GetDeviceList ();
  199.     while (aGDevice != nil)
  200.         {
  201.         if (IsActiveScreenDevice (aGDevice))
  202.             {
  203.             if (GetRectArea ((**aGDevice).gdRect) > biggestArea)
  204.                 {
  205.                 bigGDevice = aGDevice;
  206.                 biggestArea = GetRectArea ((**aGDevice).gdRect);
  207.                 }
  208.     
  209.             aGDevice = GetNextDevice (aGDevice);
  210.             }
  211.         }
  212.         
  213.     return bigGDevice;
  214. }
  215.  
  216. //    ------------------------------------------------------------------------
  217. //    Given rect r, which device does it overlap most.  An example of its use
  218. //    would be passing in (**wp->visRgn).rgnBBox to find out which device a
  219. //    window is overlapping the most, as in the case of zooming a window.
  220. //    ------------------------------------------------------------------------
  221. GDHandle GetDominantDevice (Rect *r)
  222. {
  223.     GDHandle            aGDevice;
  224.     GDHandle            bigGDevice;
  225.     Rect                screenRect;
  226.     Rect                sectRect;
  227.     long                area;
  228.     long                biggestArea = 0L;
  229.  
  230.     aGDevice = GetDeviceList ();                            //    start at begining of device list
  231.     while (aGDevice != nil)                                    //    loop if device exists
  232.         {
  233.         if (IsActiveScreenDevice (aGDevice))                //    if device is a monitor and active
  234.             {
  235.             screenRect = (**aGDevice).gdRect;                //    get the devices global rect
  236.             SectRect (&screenRect, r, §Rect);            //    get overlapping rect of device and r
  237.             
  238.             area = GetRectArea (sectRect);                    //    changed 3/21/94
  239.             if (area > biggestArea)                            //    if overlapping rect has the biggest area
  240.                 {
  241.                 bigGDevice = aGDevice;                        //    set big device to current device
  242.                 biggestArea = area;                            //    set big area to current area
  243.                 }
  244.     
  245.             aGDevice = GetNextDevice (aGDevice);            //    check next device in list
  246.             }
  247.         }
  248.         
  249.     return bigGDevice;                                        //    return device containing biggest portion of r
  250. }
  251.  
  252. //    ------------------------------------------------------------------------
  253. //    RECT STUFF
  254. //    Norman Basham, February 1994
  255. //    ------------------------------------------------------------------------
  256.  
  257. void CenterRect
  258.     (
  259.     const Rect        centerOnRect,
  260.     Rect            *rectToCenter
  261.     )
  262.     {
  263.     short            centerOnRectH;
  264.     short            centerOnRectV;
  265.     short            rectToCenterH;
  266.     short            rectToCenterV;
  267.     
  268.     if ( GetRectArea (*rectToCenter) < GetRectArea (centerOnRect) )
  269.         {
  270.         centerOnRectH = abs (centerOnRect.right - centerOnRect.left);
  271.         centerOnRectV = abs (centerOnRect.bottom - centerOnRect.top);
  272.         rectToCenterH = abs (rectToCenter->right - rectToCenter->left);
  273.         rectToCenterV = abs (rectToCenter->bottom - rectToCenter->top);
  274.         
  275.         SetRect (rectToCenter,
  276.                     centerOnRect.left + (centerOnRectH - rectToCenterH)/2,
  277.                     centerOnRect.top + (centerOnRectV - rectToCenterV)/2,
  278.                     centerOnRect.left + (centerOnRectH - rectToCenterH)/2 + rectToCenterH,
  279.                     centerOnRect.top + (centerOnRectV - rectToCenterV)/2 + rectToCenterV
  280.                 );
  281.         }
  282.     }
  283.     
  284. //    ------------------------------------------------------------------------
  285.  
  286. void GlobalToLocalRect
  287.     (
  288.     Rect            *r
  289.     )
  290.     {
  291.         GlobalToLocal (&topLeft(*r));                    //    convert first word of r
  292.         GlobalToLocal (&botRight(*r));                    //    convert second word of r
  293.     }
  294.  
  295. //    ------------------------------------------------------------------------
  296.  
  297. long GetRectArea
  298.     (
  299.     Rect             r
  300.     )
  301.     {
  302.         Rect        temp = r;
  303.     
  304.         OffsetRect (&temp, -temp.left, -temp.top);        //    rids us of neg values
  305.         return (long) temp.right * temp.bottom;            //    return width x heigth
  306.     }
  307.     
  308. //    ------------------------------------------------------------------------
  309.  
  310. void ZeroTopLeft
  311.     (
  312.     Rect            *r
  313.     )
  314.     {
  315.     OffsetRect (r, -r->left, -r->top);
  316.     }
  317.     
  318. //    ------------------------------------------------------------------------
  319.  
  320. void SetTopLeft
  321.     (
  322.     Rect            *r,
  323.     short            top,
  324.     short            left
  325.     )
  326.     {
  327.     ZeroTopLeft (r);
  328.     OffsetRect (r, left, top);
  329.     }
  330.  
  331. //    ------------------------------------------------------------------------
  332.  
  333. short abs (short i)
  334. {
  335.     if (i < 0)
  336.         return -i;
  337.         
  338.     return i;
  339. }
  340.  
  341.